# Contributor: Leo <thinkabit.ukim@gmail.com>
# Contributor: stef <l0ls0fo2i@ctrlc.hu>
# Contributor: André Klitzing <aklitzing@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=zstd
pkgver=1.5.6
pkgrel=0
pkgdesc="Zstandard - Fast real-time compression algorithm"
url="https://www.zstd.net/"
arch="all"
license="BSD-3-Clause OR GPL-2.0-or-later"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="grep meson"
checkdepends="file"
subpackages="
	$pkgname-libs
	$pkgname-static
	$pkgname-dev
	$pkgname-doc
	$pkgname-frugal
	"
source="zstd-$pkgver.tar.gz::https://github.com/facebook/zstd/archive/v$pkgver.tar.gz"
# we run the tests in build() for pgo, so it's tested from there.
# options="!check"

# secfixes:
#   1.4.9-r0:
#     - CVE-2021-24032
#   1.4.1-r0:
#     - CVE-2021-24031
#   1.3.8-r0:
#     - CVE-2019-11922

build() {
	export CFLAGS="${CFLAGS/-Os/-O3} -flto=auto -ffat-lto-objects"
	export CXXFLAGS="${CXXFLAGS/-Os/-O3} -flto=auto -ffat-lto-objects"
	export CPPFLAGS="${CPPFLAGS/-Os/-O3} -flto=auto -ffat-lto-objects"
	if [ -z "$BOOTSTRAP" ]; then
		local pgo=true
	fi
	# 2-stage pgo+lto build (non-bootstrap), standard meson usage.
	# note that with clang,
	#  llvm-profdata merge --output=output/somefilename(?) output/*.profraw
	# is needed.
	# believe it or not, this is +30% faster on x86_64 than the default makefile build (same params)..
	# maybe needs more testing
	# shellcheck disable=2046
	abuild-meson \
		--default-library=both \
		-Db_ndebug=true \
		$(test $pgo = true && echo -Db_pgo=generate) \
		-Dbin_contrib=true \
		-Dbin_programs=true \
		-Dbin_tests="$(want_check && echo true || echo false)" \
		-Dbacktrace=disabled \
		-Dmulti_thread=enabled \
		-Dlz4=disabled \
		-Dlzma=disabled \
		-Dzlib=disabled \
		build/meson output

	meson compile -C output

	if [ $pgo = true ]; then
		meson test -t 4 --no-rebuild --print-errorlogs -C output
		meson configure -Dbin_tests=false -Db_pgo=use output
		meson compile -C output
	fi
}

check() {
	# ran in build above, via pgo
	return 0
	#meson test -t 4 --no-rebuild --print-errorlogs -C output
}

package() {
	DESTDIR="$pkgdir" meson install --no-rebuild -C output
}

frugal() {
	pkgdesc="$pkgdesc (frugal cli)"

	amove usr/bin/zstd-frugal
}

sha512sums="
ca12dffd86618ca008e1ecc79056c1129cb4e61668bf13a3cd5b2fa5c93bc9c92c80f64c1870c68b9c20009d9b3a834eac70db72242d5106125a1c53cccf8de8  zstd-1.5.6.tar.gz
"
